package rexsee.communication;

import java.util.ArrayList;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;

public class RexseeSMSSender implements JavascriptInterface {

	public static final String INTERFACE_NAME = "SMSSender";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeSMSSender(childBrowser);
	}

	public static final String EXTRA_SMS_ID = "smsID";
	public static final String EXTRA_SMS_PART = "smsPart";
	public static final String EXTRA_SMS_PART_TOTAL = "smsPartTotal";

	public static final String ACTION_SMS_SENT = "SENT_SMS_ACTION";
	public static final String ACTION_SMS_DELIVERED = "DELIVERED_SMS_ACTION";

	public static final String EVENT_ONSMSSENT = "onSmsSent";
	public static final String EVENT_ONSMSDELIVERED = "onSmsDelivered";

	private final Context mContext;
	private final Browser mBrowser;

	private String mId = "";
	private int mTotalSize = 0;
	private int mSentSize = 0;

	private String getResultString(int resultCode) {
		switch (resultCode) {
			case Activity.RESULT_OK :
				return "OK";
			case SmsManager.RESULT_ERROR_GENERIC_FAILURE :
				return "RESULT_ERROR_GENERIC_FAILURE";
			case SmsManager.RESULT_ERROR_NO_SERVICE :
				return "RESULT_ERROR_NO_SERVICE";
			case SmsManager.RESULT_ERROR_NULL_PDU :
				return "RESULT_ERROR_NULL_PDU";
			case SmsManager.RESULT_ERROR_RADIO_OFF :
				return "RESULT_ERROR_RADIO_OFF";
			default :
				return "OK";
		}
	}
	private final BroadcastReceiver mSentReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				mSentSize++;
				if (mSentSize == mTotalSize) {
					context.unregisterReceiver(this);
					mBrowser.eventList.run(EVENT_ONSMSSENT, new String[]{mId, getResultString(getResultCode())});
				}
			} catch (Exception e) {
			}
		}
	};
	private final BroadcastReceiver mDeliveredReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				context.unregisterReceiver(this);
				mBrowser.eventList.run(EVENT_ONSMSDELIVERED, new String[]{mId, intent.getStringExtra("pdu")});
			} catch (Exception e) {
			}
		}
	};

	public RexseeSMSSender(Browser browser) {
		mBrowser = browser;
		mContext = browser.getContext();
		browser.eventList.add(EVENT_ONSMSSENT);
		browser.eventList.add(EVENT_ONSMSDELIVERED);
	}

	//JavaScript Interface

	public void send(String phoneNumber, String message) {
		send(null, phoneNumber, message);
	}
	public void send(String id, String phoneNumber, String message) {

		SmsManager sms = SmsManager.getDefault();
		ArrayList<String> msgs = sms.divideMessage(message);
		mTotalSize = msgs.size();
		mSentSize = 0;
		mId = (id != null && !id.trim().equals("")) ? id : null;

		try {
			mContext.unregisterReceiver(mSentReceiver);
			mContext.unregisterReceiver(mDeliveredReceiver);
		} catch (Exception e) {
		}

		ArrayList<PendingIntent> sentIntents = null;
		ArrayList<PendingIntent> deliveryIntents = null;
		if (mId != null) {
			sentIntents = new ArrayList<PendingIntent>();
			deliveryIntents = new ArrayList<PendingIntent>();
			PendingIntent deliverPI = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_DELIVERED), 0);
			for (int i = 0; i < mTotalSize; i++) {
				PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_SENT + "_" + i), 0);
				sentIntents.add(sentPI);
				mContext.registerReceiver(mSentReceiver, new IntentFilter(ACTION_SMS_SENT + "_" + i));
				deliveryIntents.add(deliverPI);
			}
			mContext.registerReceiver(mDeliveredReceiver, new IntentFilter(ACTION_SMS_DELIVERED));
		}
		sms.sendMultipartTextMessage(phoneNumber, null, msgs, sentIntents, deliveryIntents);

	}

}
